package com.authine.cloudpivot.web.api.secondopen.controller;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.engine.api.model.organization.DepartmentModel;
import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectModel;
import com.authine.cloudpivot.engine.api.model.runtime.SelectionValue;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.secondopen.dao.NewsMapper;
import com.authine.cloudpivot.web.api.secondopen.dao.UserMapper;
import com.authine.cloudpivot.web.api.secondopen.domain.AttachModel;
import com.authine.cloudpivot.web.api.secondopen.domain.Attachment;
import com.authine.cloudpivot.web.api.secondopen.domain.NewsModel;
import com.authine.cloudpivot.web.api.secondopen.domain.NewsTypeModel;
import com.authine.cloudpivot.web.api.secondopen.domain.RemarkModel;
import com.authine.cloudpivot.web.api.secondopen.service.NewsService;
import com.authine.cloudpivot.web.api.secondopen.utils.CommUtil;
import com.authine.cloudpivot.web.api.secondopen.utils.JsonUtils;
import com.authine.cloudpivot.web.api.secondopen.vo.AttachmentVO;
import com.authine.cloudpivot.web.api.secondopen.vo.MixNoticeAndNewsVO;
import com.authine.cloudpivot.web.api.secondopen.vo.NewsTypeVO;
import com.authine.cloudpivot.web.api.secondopen.vo.NewsVO;
import com.authine.cloudpivot.web.api.secondopen.vo.Notice1VO;
import com.authine.cloudpivot.web.api.secondopen.vo.PictrueVO;
import com.authine.cloudpivot.web.api.secondopen.vo.RemarkVO;
import com.authine.cloudpivot.web.api.view.PageVO;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jsoup.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "二开接口", tags = "新闻")
@RestController
@RequestMapping("/api/news")
public class NewsController extends BaseController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    private CommUtil commUtil;


    @ApiOperation(value = "首页获取新闻列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsType", value = "新闻类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/getIndexNewsList")
    public ResponseResult<PageVO<NewsVO>> getIndexNewsList(@RequestParam String newsType, @RequestParam String page, @RequestParam String size) {

        String XWFB = getBizObjectFacade().getTableName("XWFB");
        int startPage = "".equals(page) ? 0 : Integer.parseInt(page);
        int pageSize = "".equals(size) ? 10 : Integer.parseInt(size);
        Pageable pageable = PageRequest.of(startPage, pageSize);
        String userId = this.getUserId();
        PageInfo<NewsModel> newsList = newsService.getIndexNewsList(startPage, pageSize, newsType, null, userId, XWFB, false);
        if (newsList == null) {
            return getOkResponseResult(null, "查询" + newsType + "数据为空");
        }
        List<NewsModel> newsModels = newsList.getList();
        List<NewsVO> newsVOs = new ArrayList<NewsVO>();

        String id = "";
        NewsVO newsVO;
        for (NewsModel next : newsModels) {
            newsVO = new NewsVO();
            id = next.getId();
            //主建id
            newsVO.setId(next.getId());
            //详细内容
            newsVO.setContent(next.getContent());
            //发布时间
            newsVO.setPublishTime(next.getPublishTime());
            //标题
            newsVO.setTitle(next.getTitle());
            //置顶
            newsVO.setTop(next.getTop());
            //集团或本司
            newsVO.setGroup(next.getIfShow());
            //新闻类型
            newsVO.setNewsType(next.getNewsType());
            //写稿人
            newsVO.setCreater(getOrganizationFacade().getUserById(next.getCreater()).getName());
            newsVOs.add(newsVO);
        }
        Page<NewsVO> newsVOPage = new PageImpl<>(newsVOs, pageable, newsList.getTotal());
        return getOkResponseResult(new PageVO<>(newsVOPage), "查询" + newsType + "列表成功");

    }



    @ApiOperation(value = "获取新闻详情页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "新闻主建ID", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/getNewsDetailById")
    public ResponseResult<MixNoticeAndNewsVO> getNewsDetailById(@RequestParam String id) {
        String XWFB = getBizObjectFacade().getTableName("XWFB");
        String RECORD = getBizObjectFacade().getTableName("record");
        String REMARK = getBizObjectFacade().getTableName("remark");
        NewsModel news = newsService.getNewsById(id, XWFB);
        BizObjectModel model = getBizObjectFacade().getBizObject("XWFB", id);
        if (news == null) {
            return getNoticeDetailById(id);
        }
        id = news.getId();
        if (StringUtil.isBlank(id)) {
            return getOkResponseResult(null, "查询新闻数据为空");
        }
        String userId = this.getUserId();
        MixNoticeAndNewsVO newsVO = new MixNoticeAndNewsVO();
        //主建id
        newsVO.setId(news.getId());
        //发布时间
        newsVO.setPublishTime(news.getPublishTime());
        //标题
        newsVO.setTitle(news.getTitle());
        //新闻类型
        newsVO.setNewsType(news.getNewsType());
        //置顶
        newsVO.setTop(news.getTop());
        //集团或本司
        newsVO.setGroup(news.getIfShow());
        //写稿人
        //修改日期
        if (StringUtils.isNotBlank(news.getAuthor())) {
            List<Map<String, Object>> list = JsonUtils.GetJsonListByString(news.getAuthor());
            String authorId = (String) list.get(0).get("id");
            newsVO.setCreater(getOrganizationFacade().getUserById(authorId).getName());
        } else {
            newsVO.setCreater(getOrganizationFacade().getUserById(news.getCreater()).getName());
        }
        //添加创建人图片
        UserModel user = userMapper.selectRefId("h_org_user", news.getCreater());
        newsVO.setCreaterPic(user.getImgUrl());
        //详细内容
        newsVO.setContent(news.getContent());
        //已读数
//        newsVO.setViewNum(newsService.getReadNum("R", id, null, RECORD));
        //点赞数
//        newsVO.setLikeNum(newsService.getReadNum("L", id, null, RECORD));
        //评论数
//        newsVO.setCommentNum(newsService.getComentNum(id, REMARK));
        //是否已阅
        newsVO.setIfView(true);
        //是否点赞
//        newsVO.setIfLike(newsService.getFlags("L", id, userId, RECORD));
        //附件
        List<AttachmentVO> AttachmentVOs = new ArrayList<AttachmentVO>();
        List<AttachModel> attachments = newsService.getAttchList(id);

        if (!CollectionUtils.isEmpty(attachments)) {
            AttachmentVO attachmentVO;
            for (AttachModel attach : attachments) {
                attachmentVO = new AttachmentVO();
                //附件主建
                attachmentVO.setId(attach.getId());
                //附件名称
                attachmentVO.setName(attach.getName());
                //附件资源Id
                attachmentVO.setRefId(attach.getRefId());
                if (StringUtils.isNotBlank(attach.getMimeType())) {
                    attachmentVO.setMimeType(attach.getMimeType().split("/")[1]);
                }
                attachmentVO.setFileSize(attach.getFileSize());
                AttachmentVOs.add(attachmentVO);
            }
        }
        newsVO.setAttachs(AttachmentVOs);
        List<RemarkVO> remarkVOs = new ArrayList<RemarkVO>();
//        List<RemarkModel> remarks = newsService.getRemarkList(userId, id, REMARK);
//        if (!CollectionUtils.isEmpty(remarks)) {
//            RemarkVO remarkVO;
//            String remarkId = "";
//            for (RemarkModel remark : remarks) {
//                remarkVO = new RemarkVO();
//                //主建Id
//                remarkId = remark.getId();
//                remarkVO.setId(remarkId);
//                //评论人
//                remarkVO.setCreater(getOrganizationFacade().getUserById(remark.getCreater()).getName());
//                remarkVO.setCreaterId(remark.getCreater());
//                remarkVO.setDelFlag(userId.equals(remark.getCreater()));
//                //评论内容
//                remarkVO.setContent(remark.getContent());
//                //评论时间
//                remarkVO.setModifiedTime(remark.getModifiedTime());
//                //点赞数
////                remarkVO.setLikeNum(newsService.getReadNum("L", remarkId, null, RECORD));
//                //是否点赞
////                remarkVO.setIfLike(newsService.getFlags("L", remarkId, userId, RECORD));
//
//                //添加头像图片
//                UserModel h_org_user = userMapper.selectRefId("h_org_user", remark.getCreater());
//                remarkVO.setPictrue(h_org_user.getImgUrl());
//
//                remarkVOs.add(remarkVO);
//            }
//        }
        newsVO.setRemarks(remarkVOs);
        //上一页
        String previousId = newsService.getPreviousId(news.getTop(), userId, news.getPublishTime(), news.getNewsType(), XWFB);
        //下一页
        String nextId = newsService.getNextId(news.getTop(), userId, news.getPublishTime(), news.getNewsType(), XWFB);
        newsVO.setPreviousId(previousId);
        newsVO.setNextId(nextId);
//        if (!newsService.getFlags("R", id, userId, RECORD)) {
//            newsService.setNumber(news.getId(), news.getNewsType(), "record", userId, "R");
//        }
        return getOkResponseResult(newsVO, "查询" + news.getNewsType() + "详情成功");
    }


    @ApiOperation(value = "获取通知公告详情页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主建ID", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/getNoticeDetailById")
    public ResponseResult<MixNoticeAndNewsVO> getNoticeDetailById(@RequestParam String id) {
        String GGFB = getBizObjectFacade().getTableName("GGFB");
        String RECORD = getBizObjectFacade().getTableName("record");
        NewsModel news = newsService.getNewsById(id, GGFB);
        String userId = this.getUserId();
        id = news.getId();
        if (StringUtil.isBlank(id)) {
            return getOkResponseResult(null, "查询通知公告数据为空");
        }
        MixNoticeAndNewsVO notice1VO = new MixNoticeAndNewsVO();
        //主建id
        notice1VO.setId(news.getId());
        //发布时间
        notice1VO.setPublishTime(news.getPublishTime());
        //标题
        notice1VO.setTitle(news.getTitle());
        //新闻类型
        notice1VO.setNewsType(news.getNewsType());
        //置顶
        notice1VO.setTop(news.getTop());
        //集团或本司
        notice1VO.setGroup(news.getIfShow());
        //写稿人
        notice1VO.setCreater(getOrganizationFacade().getUserById(news.getCreater()).getName());
        //添加头像图片
        UserModel h_org_user = userMapper.selectRefId("h_org_user", news.getCreater());
        notice1VO.setCreaterPic(h_org_user.getImgUrl());
        //详细内容
        notice1VO.setContent(news.getContent());
        //已读数
//        notice1VO.setViewNum(newsService.getReadNum("R", id, null, RECORD));
        //是否已读
        notice1VO.setIfView(true);
        //附件
        List<AttachmentVO> AttachmentVOs = new ArrayList<AttachmentVO>();
        List<AttachModel> attachments = newsService.getAttchList(id);
        if (!CollectionUtils.isEmpty(attachments)) {
            AttachmentVO attachmentVO;
            for (AttachModel attach : attachments) {
                attachmentVO = new AttachmentVO();
                //附件主建
                attachmentVO.setId(attach.getId());
                //附件名称
                attachmentVO.setName(attach.getName());
                //附件资源Id
                attachmentVO.setRefId(attach.getRefId());
                if (StringUtils.isNotBlank(attach.getMimeType())) {
                    attachmentVO.setMimeType(attach.getMimeType().split("/")[1]);
                }
                attachmentVO.setFileSize(attach.getFileSize());
                AttachmentVOs.add(attachmentVO);
            }
        }
        notice1VO.setAttachs(AttachmentVOs);
        //上一页
        String previousId = newsService.getPreviousId(news.getTop(), userId, news.getPublishTime(), news.getNewsType(), GGFB);
        //下一页
        String nextId = newsService.getNextId(news.getTop(), userId, news.getPublishTime(), news.getNewsType(), GGFB);
        notice1VO.setPreviousId(previousId);
        notice1VO.setNextId(nextId);

//        if (!newsService.getFlags("R", id, userId, RECORD)) {
//            newsService.setNumber(news.getId(), news.getNewsType(), "record", userId, "R");
//        }
        return getOkResponseResult(notice1VO, "查询" + news.getNewsType() + "详情成功");
    }




    @ApiOperation(value = "获取新闻评论列表")
    @GetMapping("/getNewsRemarkList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "新闻编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = false, dataType = "String", paramType = "query")
    })
    public ResponseResult<PageVO<RemarkVO>> getNewsRemarkList(@RequestParam String id, @RequestParam String page, @RequestParam String size) {

        String RECORD = getBizObjectFacade().getTableName("record");
        String REMARK = getBizObjectFacade().getTableName("remark");
        int startPage = "".equals(page) ? 0 : Integer.parseInt(page);
        int pageSize = "".equals(size) ? 5 : Integer.parseInt(size);

        Pageable pageable = PageRequest.of(startPage, pageSize);
        String userId = getUserId();
        PageInfo<RemarkModel> newsList = newsService.getNewsRemarkList(startPage, pageSize, REMARK, id, userId);
        if (newsList == null) {
            return getOkResponseResult(null, "查询评论数据为空");
        }
        List<RemarkModel> newsModels = newsList.getList();
        List<RemarkVO> newsVOs = new ArrayList<RemarkVO>();
        RemarkVO remarkVO;
        for (RemarkModel next : newsModels) {
            remarkVO = new RemarkVO();
            //主建id
            remarkVO.setId(next.getId());

            //详细内容
            remarkVO.setContent(next.getContent());
            //评论人
            remarkVO.setCreater(getOrganizationFacade().getUserById(next.getCreater()).getName());
            remarkVO.setCreaterId(next.getCreater());
            remarkVO.setDelFlag(userId.equals(next.getCreater()));
            //评论时间
            remarkVO.setModifiedTime(next.getModifiedTime());
            //点赞数
            remarkVO.setLikeNum(newsService.getReadNum("L", next.getId(), null, RECORD));
            //是否点赞
            remarkVO.setIfLike(newsService.getFlags("L", next.getId(), this.getUserId(), RECORD));
            //添加头像图片
            UserModel h_org_user = userMapper.selectRefId("h_org_user", next.getCreater());
            remarkVO.setPictrue(h_org_user.getImgUrl());
            newsVOs.add(remarkVO);
        }
        Page<RemarkVO> newsVOPage = new PageImpl<>(newsVOs, pageable, newsList.getTotal());
        return getOkResponseResult(new PageVO<>(newsVOPage), "查询新闻评论列表成功");
    }




    @ApiOperation(value = "点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主建ID", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/addLikeNum")
    public ResponseResult<Object> addLikeNum(@RequestParam String id) {
        String objectId = newsService.setNumber(id, null, "record", this.getUserId(), "L");
        if (StringUtils.isBlank(objectId)) {
            return getOkResponseResult(null, "点赞失败");
        }
        return getOkResponseResult("Id=" + objectId, "点赞成功");
    }


    @ApiOperation(value = "删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论主建ID", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/deleteRemark")
    public ResponseResult<?> deleteRemark(@RequestParam String id) {
        String tableName = getBizObjectFacade().getTableName("remark");
        newsMapper.deleteRemark(id, tableName);
        return getOkResponseResult("删除评论成功！");
    }

    @ApiOperation(value = "添加评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主建ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/addRemark")
    public ResponseResult<List<RemarkVO>> addRemark(@RequestParam String id, @RequestParam String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("bizObjectId", id);
        map.put("content", content);
        BizObjectModel bo = new BizObjectModel("remark", map, Boolean.FALSE);
        String userId = this.getUserId();
        String objectId = getBizObjectFacade().saveBizObject(userId, bo, Boolean.FALSE);
        if (StringUtils.isBlank(objectId)) {
            return getOkResponseResult(null, "添加评论失败");
        }
        List<RemarkVO> remarkVOs = new ArrayList<RemarkVO>();
        String REMARK = getBizObjectFacade().getTableName("remark");
        String RECORD = getBizObjectFacade().getTableName("record");
        List<RemarkModel> remarks = newsService.getRemarkList(userId, id, REMARK);
        if (!CollectionUtils.isEmpty(remarks)) {
            RemarkVO remarkVO;
            String remarkId = "";
            for (RemarkModel remark : remarks) {
                remarkVO = new RemarkVO();
                //主建Id
                remarkId = remark.getId();
                remarkVO.setId(remarkId);
                //评论人
                remarkVO.setCreater(getOrganizationFacade().getUserById(remark.getCreater()).getName());
                remarkVO.setCreaterId(remark.getCreater());
                remarkVO.setDelFlag(userId.equals(remark.getCreater()));
                //评论内容
                remarkVO.setContent(remark.getContent());
                //评论时间
                remarkVO.setModifiedTime(remark.getModifiedTime());
                //点赞数
                remarkVO.setLikeNum(newsService.getReadNum("L", remarkId, null, RECORD));
                //是否点赞
                remarkVO.setIfLike(newsService.getFlags("L", remarkId, userId, RECORD));
                //添加头像图片
                UserModel h_org_user = userMapper.selectRefId("h_org_user", remark.getCreater());
                remarkVO.setPictrue(h_org_user.getImgUrl());
                remarkVOs.add(remarkVO);
            }
        }
        return getOkResponseResult(remarkVOs, "添加评论成功");
    }


    @ApiOperation(value = "获取新闻目录")
    @GetMapping("/getNewsTypeList")
    public ResponseResult<List<NewsTypeVO>> getNewsTypeList() {
        String newsTable = getBizObjectFacade().getTableName("NewsTypeManager");
        String GGFB = getBizObjectFacade().getTableName("GGFB");
        String userId = this.getUserId();

        List<NewsTypeVO> List = new ArrayList<>();
        List<NewsTypeModel> newsList = newsService.getNewsTypeList(newsTable);
        if (newsList != null) {
            NewsTypeVO newsVO;
            for (NewsTypeModel next : newsList) {
                newsVO = new NewsTypeVO();
                newsVO.setId(next.getId());
                newsVO.setNewsType(next.getNewsType());
                newsVO.setBelong(next.getBelong());
                if ("公告".equals(next.getBelong())) {
//                    newsVO.setUnReadnum(newsService.getTypeUnReadNum(next.getNewsType(), userId, GGFB));
                    newsVO.setUnReadnum(null);
                }
                List.add(newsVO);
            }
        }
        return getOkResponseResult(List, "查询目录成功");
    }



    @ApiOperation(value = "获取通知公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsType", value = "公告类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sortStr", value = "排序字段json格式，例：{\"title\":\"asc\",\"publishTime\":\"desc\"}）", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deptId", value = "部门id(用于子门户查询)", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/getNoticesList")
    public ResponseResult<?> getNoticesList(@RequestParam(required = false) String newsType, @RequestParam(required = false) String title, @RequestParam(required = false) String sortStr, @RequestParam String page, @RequestParam String size, @RequestParam(required = false) String deptId, @RequestParam(required = false) String isImportant, @RequestParam(required = false) String fromTime, @RequestParam(required = false) String toTime) {
        String GGFB = getBizObjectFacade().getTableName("GGFB");
        String RECORD = getBizObjectFacade().getTableName("record");
        int startPage = "".equals(page) ? 0 : Integer.parseInt(page);
        int pageSize = "".equals(size) ? 10 : Integer.parseInt(size);
        Pageable pageable = PageRequest.of(startPage, pageSize);
        String userId = this.getUserId();
        //新增排序
        Map<String, String> sort = new HashMap<>();
        if (StringUtils.isNotBlank(sortStr)) {
            sort = (Map) JSON.parse(sortStr);
        }
        PageInfo<NewsModel> newsList = null;
        if (!StringUtils.isBlank(deptId)) {
            List<String> deptIds = commUtil.getDeptIdsByDeptId(deptId);
            newsList = newsService.getNewsListByDeptIds(startPage, pageSize, newsType, title, deptIds, GGFB, true);
        } else {

            newsList = newsService.getNewsList2(startPage, pageSize, newsType, title, sort, userId, GGFB, true, isImportant, fromTime, toTime);
        }
        if (newsList == null) {
            return getOkResponseResult(null, "查询" + newsType + "数据为空");
        }
        List<NewsModel> newsModels = newsList.getList();
        List<Notice1VO> notice1VOS = new ArrayList<Notice1VO>();

        String id = "";
        Notice1VO notice1VO;
        for (NewsModel next : newsModels) {
            notice1VO = new Notice1VO();
            id = next.getId();
            //主建id
            notice1VO.setId(next.getId());
            //已读数
//            notice1VO.setViewNum(newsService.getReadNum("R", id, null, RECORD));
            //是否已读
//            notice1VO.setIfView(newsService.getFlags("R", id, userId, RECORD));
            //详细内容
            notice1VO.setContent(next.getContent());
            //发布时间
            notice1VO.setPublishTime(next.getPublishTime());
            //标题
            notice1VO.setTitle(next.getTitle());
            //置顶
            notice1VO.setTop(next.getTop());
            //集团或本司
            notice1VO.setGroup(next.getIfShow());
            //新闻类型
            notice1VO.setNewsType(next.getNewsType());
            //写稿人
            UserModel userModel = getOrganizationFacade().getUserById(next.getCreater());
            notice1VO.setCreater(userModel.getName());

            //设置移动端展示图片
            if (StringUtils.isNotBlank(next.getId())) {
                List<Attachment> attachments = newsMapper.selectAttachmentByNewsId(GGFB, next.getId());
                if (attachments != null && attachments.size() > 0 && attachments.get(0) != null) {
                    notice1VO.setPictrue(attachments.get(0).getRefId());
                }
            }

            notice1VOS.add(notice1VO);
        }
        PageInfo newsVOPage = newsList;
        newsVOPage.setList(notice1VOS);
        return ResponseResult.<PageInfo>builder().errcode(200L).data(newsVOPage).build();

    }



    @ApiOperation(value = "获取首页通知公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsType", value = "公告类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/getIndexNoticesList")
    public ResponseResult<PageVO<Notice1VO>> getIndexNoticesList(@RequestParam String newsType, @RequestParam String page, @RequestParam String size) {
        String GGFB = getBizObjectFacade().getTableName("GGFB");
        int startPage = "".equals(page) ? 0 : Integer.parseInt(page);
        int pageSize = "".equals(size) ? 10 : Integer.parseInt(size);
        Pageable pageable = PageRequest.of(startPage, pageSize);
        String userId = this.getUserId();
        PageInfo<NewsModel> newsList = newsService.getNewsList(startPage, pageSize, newsType, null, userId, GGFB, false);
        if (newsList == null) {
            return getOkResponseResult(null, "查询" + newsType + "数据为空");
        }
        List<NewsModel> newsModels = newsList.getList();
        List<Notice1VO> notice1VOS = new ArrayList<Notice1VO>();

        String id = "";
        Notice1VO notice1VO;
        for (NewsModel next : newsModels) {
            notice1VO = new Notice1VO();
            id = next.getId();
            //主建id
            notice1VO.setId(next.getId());
            //详细内容
            notice1VO.setContent(next.getContent());
            //发布时间
            notice1VO.setPublishTime(next.getPublishTime());
            //标题
            notice1VO.setTitle(next.getTitle());
            //置顶
            notice1VO.setTop(next.getTop());
            //集团或本司
            notice1VO.setGroup(next.getIfShow());
            //新闻类型
            notice1VO.setNewsType(next.getNewsType());
            //写稿人
            UserModel userModel = getOrganizationFacade().getUserById(next.getCreater());
            notice1VO.setCreater(userModel.getName());
            notice1VOS.add(notice1VO);
        }
        Page<Notice1VO> noticeVOPage = new PageImpl<>(notice1VOS, pageable, newsList.getTotal());
        return getOkResponseResult(new PageVO<>(noticeVOPage), "查询" + newsType + "列表成功");
    }


   @ApiOperation(value = "获取评论审批列表")
    @GetMapping("/getRemarkList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = false, dataType = "String", paramType = "query"),
    })
    public ResponseResult<PageVO<RemarkVO>> getRemarkList(@RequestParam String page, @RequestParam String size) {

        String RECORD = getBizObjectFacade().getTableName("remark");
        int startPage = "".equals(page) ? 0 : Integer.parseInt(page);
        int pageSize = "".equals(size) ? 5 : Integer.parseInt(size);

        Pageable pageable = PageRequest.of(startPage, pageSize);

        PageInfo<RemarkModel> newsList = newsService.getUnRemarkList(startPage, pageSize, RECORD);
        if (newsList == null) {
            return getOkResponseResult(null, "查询评论数据为空");
        }
        List<RemarkModel> newsModels = newsList.getList();
        List<RemarkVO> newsVOs = new ArrayList<RemarkVO>();
        RemarkVO newsVO;
        for (RemarkModel next : newsModels) {
            newsVO = new RemarkVO();
            //主建id
            newsVO.setId(next.getId());
            BizObjectModel model = getBizObjectFacade().getBizObject("XWFB", next.getBizObjectId());
            //标题
            if (model == null) {
                getBizObjectFacade().removeBizObject(next.getCreater(), "remark", next.getId());
            } else {
                if (!model.getData().isEmpty()) {
                    if (model.getData().containsKey("title")) {
                        newsVO.setTitle(model.getData().get("title").toString());
                    } else {
                        continue;
                    }
                }
            }
            //详细内容
            newsVO.setContent(next.getContent());
            //评论人
            newsVO.setCreater(getOrganizationFacade().getUserById(next.getCreater()).getName());
            //评论时间
            newsVO.setModifiedTime(next.getModifiedTime());
            newsVOs.add(newsVO);
        }
        Page<RemarkVO> newsVOPage = new PageImpl<>(newsVOs, pageable, newsList.getTotal());
        return getOkResponseResult(new PageVO<>(newsVOPage), "查询评论列表成功");
    }




    @ApiOperation(value = "获取集团新闻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsType", value = "新闻类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/getSlideshow")
    public ResponseResult<?> getHeadimageList(@RequestParam String newsType, @RequestParam String page, @RequestParam String size) {

        String XWFB = getBizObjectFacade().getTableName("XWFB");
        int startPage = "".equals(page) ? 0 : Integer.parseInt(page);
        int pageSize = "".equals(size) ? 10 : Integer.parseInt(size);
        Pageable pageable = PageRequest.of(startPage, pageSize);
        PageInfo newsList = null;
        if ("卓越石药人".equals(newsType)) {
            newsList = newsService.getNewsList(startPage, pageSize, newsType, null, this.getUserId(), XWFB, false);
        } else {
            newsList = newsService.getImageNewsList(startPage, pageSize, newsType, this.getUserId(), XWFB);
        }
        if (newsList == null) {
            return getOkResponseResult(null, "查询" + newsType + "数据为空!");
        }
        List<NewsModel> newsModels = newsList.getList();
        List<PictrueVO> newsVOs = new ArrayList<PictrueVO>();
        String id = "";
        PictrueVO newsVO;
        for (NewsModel next : newsModels) {
            newsVO = new PictrueVO();
            id = next.getId();
            //主建id
            newsVO.setId(next.getId());
            //新闻类型
            newsVO.setNewsType(newsType);
            if (newsType.equals("卓越石药人") && !StringUtil.isBlank(next.getAuthor())) {
                List<SelectionValue> createrList = JSON.parseArray(next.getAuthor(), SelectionValue.class);
                UserModel user = getOrganizationFacade().getUserById(createrList.get(0).getId());
                if (user != null) {
                    newsVO.setAuthor(user.getName());
                    newsVO.setAppellation(user.getAppellation());
                    DepartmentModel department = getOrganizationFacade().getDepartment(user.getDepartmentId());
                    if (department != null) {
                        newsVO.setDepart(department.getName());
                    }
                }
            }
            //详细内容
            newsVO.setContent(next.getContent());
            //发布时间
            newsVO.setPublishTime(next.getPublishTime());
            //标题
            newsVO.setTitle(next.getTitle());
            //置顶
            newsVO.setTop(next.getTop());
            //集团或本司
            newsVO.setGroup(next.getIfShow());
            //写稿人
            newsVO.setCreater(getOrganizationFacade().getUserById(next.getCreater()).getName());

            List<AttachModel> attachments = newsService.getPicList(id);
            if (!CollectionUtils.isEmpty(attachments)) {
                //资源id
                newsVO.setPicRefId(attachments.get(0).getRefId());
                //图片名称
                newsVO.setPicName(attachments.get(0).getName());

            }
            newsVOs.add(newsVO);
        }
        newsList.setList(newsVOs);
        return getOkResponseResult(newsList, "查询" + newsType + "数据成功!");

    }


    @ApiOperation(value = "获取新闻列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsType", value = "新闻类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sortStr", value = "排序字段json格式，例：{\"title\":\"asc\",\"publishTime\":\"desc\"}）", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/getNewsList")
    public ResponseResult<?> getNewsList(@RequestParam String newsType, @RequestParam(required = false) String title, @RequestParam(required = false) String sortStr, @RequestParam String page, @RequestParam String size) {

        if (!"新闻".equals(newsType)) {
            return getNoticesList(newsType, title, sortStr, page, size, null, null, null, null);
        }

        String XWFB = getBizObjectFacade().getTableName("XWFB");
        String RECORD = getBizObjectFacade().getTableName("record");
        String REMARK = getBizObjectFacade().getTableName("remark");
        int startPage = "".equals(page) ? 0 : Integer.parseInt(page);
        int pageSize = "".equals(size) ? 10 : Integer.parseInt(size);
        //新增排序
        Map<String, String> sort = new HashMap<>();
        if (StringUtils.isNotBlank(sortStr)) {
            sort = (Map) JSON.parse(sortStr);
        }

        String userId = this.getUserId();
        long startTime = System.currentTimeMillis(); //获取开始时间
        PageInfo<NewsModel> newsList = newsService.getNewsList2(startPage, pageSize, newsType, title, sort, userId, XWFB, true, null, null, null);
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms"); //输出程序运行时间
        if (newsList == null) {
            return getOkResponseResult(null, "查询" + newsType + "数据为空");
        }
        List<NewsModel> newsModels = newsList.getList();
        List<NewsVO> newsVOs = new ArrayList<NewsVO>();

        String id = "";
        NewsVO newsVO;
        for (NewsModel next : newsModels) {
            newsVO = new NewsVO();
            id = next.getId();
            //主建id
            newsVO.setId(next.getId());
            //已读数
//            newsVO.setViewNum(newsService.getReadNum("R", id, null, RECORD));
            //点赞数
//            newsVO.setLikeNum(newsService.getReadNum("L", id, null, RECORD));
            //评论数
//            newsVO.setCommentNum(newsService.getComentNum(id, REMARK));
            //是否已读
//            newsVO.setIfView(newsService.getFlags("R", id, userId, RECORD));
//            newsVO.setIfLike(newsService.getFlags("L", id, userId, RECORD));
            //详细内容
            newsVO.setContent(next.getContent());
            //发布时间
            newsVO.setPublishTime(next.getPublishTime());
            //标题
            newsVO.setTitle(next.getTitle());
            //置顶
            newsVO.setTop(next.getTop());
            //集团或本司
            newsVO.setGroup(next.getIfShow());
            //新闻类型
            newsVO.setNewsType(next.getNewsType());
            //写稿人
            //修改日期
            if (StringUtils.isNotBlank(next.getAuthor())) {
                List<Map<String, Object>> list = JsonUtils.GetJsonListByString(next.getAuthor());
                String authorId = (String) list.get(0).get("id");
                newsVO.setCreater(getOrganizationFacade().getUserById(authorId).getName());

            } else {
                newsVO.setCreater(getOrganizationFacade().getUserById(next.getCreater()).getName());
            }
            //设置移动端展示图片
            if (StringUtils.isNotBlank(next.getId())) {
                List<Attachment> attachments = newsMapper.selectAttachmentByNewsId(XWFB, next.getId());
                if (attachments != null && attachments.size() > 0 && attachments.get(0) != null) {
                    newsVO.setPictrue(attachments.get(0).getRefId());
                }
            }

            newsVOs.add(newsVO);
        }
        PageInfo newsVOPage = newsList;
        newsVOPage.setList(newsVOs);
        return ResponseResult.<PageInfo>builder().errcode(200L).data(newsVOPage).build();
    }

}
